package root.communication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class MessageProcessor {
	
	public final static String UNKNOWN_QUERY="unknown";
	private final static String detailDelimeter = "" + (char)31;
	private final static String bigDelimeter = "" + (char)30;
	public static BufferedReader in = null;
	
	public static String process(String s)
	{
		String [] args=s.split(":");
		if (args[0].equals("query"))
			return processQuery(args[1]);
		if (args[0].equals("countclients"))
			return args[1];
		if (args[0].equals("listclients"))
		{
			if (args.length > 1)
				listClients(args[1]);
			else
				System.out.println("No clients available");
			return null;
		}
		if (args[0].equals("done")||args[0].equals("bash"))
		{
			if (args.length > 1)
				//printArgs(args[1]);
				printArgs(s.substring("done:".length()));
			return null;
		}
		if (args[0].equals("file"))
			return downloadFile(args[1])?"Downloading finished":"Downloading failed";
		if (args[0].equals("globallogs"))
		{
			printGlobalLogs(s.substring(11));
			return null;
		}
		else
			System.out.println(s);
		return UNKNOWN_QUERY;
	}
	
	private static void printGlobalLogs(String in)
	{
		String [] rows = in.split(bigDelimeter);
		String [] strs ;
		for (String row : rows)
		{
			strs = row.split(detailDelimeter);
			for (String str : strs)
				System.out.print(str+"\t");
			System.out.println();
		}
	}
	
	private static boolean downloadFile(String filename)
	{
		System.out.println("Downloading " + filename);
		FileOutputStream fWriter = null;
		try
		{
			fWriter = new FileOutputStream(filename);
		}
		catch (Exception e)
		{
			System.err.println("Unable to write to a file");
			e.printStackTrace();
			System.exit(-1);
		}
		String input;
		try
		{
			while ((input = in.readLine())!=null && !input.equals("file:finished"))
			{
				fWriter.write(Integer.parseInt(input));
			}
			fWriter.close();
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	private static void printArgs(String args)
	{
		String [] entries;
		if (args.contains(detailDelimeter))
			entries = args.split(detailDelimeter);
		else
		{
			entries = new String [1];
			entries[0]=args;
		}
		for (String entry : entries)
			System.out.println("\t"+entry);
	}
	private static void listClients(String list)
	{
		String []entries = list.split(bigDelimeter);
		String [] args;
		for (String entry : entries)
		{
			args = entry.split(detailDelimeter);
			for (String arg : args)
				System.out.print(arg+'\t');
			System.out.println();
		}
	}
	
	private static String processQuery(String s)
	{
		if (s.equals("clienttype"))
		{
			return "clienttype:root";
		}
		return UNKNOWN_QUERY;
	}
	
	public static String countClientsQuery()
	{
		return "query:countclients";
	}
	
	public static String listClientsQuery()
	{
		return "query:listclients";
	}
	
	public static String executeCommand(String command)
	{
		return "exec:"+command;
	}
	
	public static String executeBashCommand(String command)
	{
		return "bash:"+command;
	}
	public static String globalLogsQuery(int num)
	{
		return "query:globallogs:"+num;
	}
	
}
